package com.example.demo.controller;

import com.example.demo.core.MyException;
import com.example.demo.core.MyException1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@Api(value = "HelloController", description = "hello模块接口")
@RestController
@RequestMapping("/hello")
public class HelloController {

    public static final Logger logger = LogManager.getLogger(HelloController.class);

    @Value("${appName}")
    private String appName;

    @ApiOperation(value = "获取hello信息", notes = "获取hello信息n")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String hello() {

        return "Hello spring boot!123123143" + appName;
    }


    @RequestMapping(value = "/login/{username}", method = RequestMethod.GET)
    public ModelAndView Login(@PathVariable("username") String username) {
        ModelAndView view = new ModelAndView("Login");
        view.addObject("username", username);
        return view;
    }

    @RequestMapping(value = "/test/{username}", method = RequestMethod.GET)
    public Object TestError(@PathVariable("username") String username) throws Exception {
        logger.error("E" + username);
        logger.info("I" + username);
        logger.debug("D" + username);
        logger.fatal("F" + username);
        logger.warn("W" + username);

        if (username.equals("chenhegang")) {
            throw new MyException("102", "chenhegang MyException自定义错误");
        }

        if (username.equals("chenhegang1")) {
            throw new MyException1("104", "chenhegang MyException1自定义错误");
        }

        return String.format("hello %s", username);
    }
}